import requests

def handler_factory(model:str,base_url:str="http://localhost:11434"):
    """
    Factory function to create a handler for the specified model.
    """
    def handler(query:str):
        url=f"{base_url}/api/chat"
        data={
            "model":model,
            "messages":[{"role":"user","content":query}],
            "stream":True
        }
        try:
            response=requests.post(url,json=data,stream=True)
            for line in response.iter_lines():
                if line:
                    yield line.decode("utf-8")
        except requests.RequestException as e:
            yield f"[Ollama Error]: {str(e)}"
    return handler